package com.lanhe.common.utils.zhifubao;


import com.alibaba.fastjson2.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;

import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.lanhe.common.constant.HttpStatus;
import com.lanhe.common.exception.ServiceException;
import com.lanhe.common.utils.zhifubao.vo.ZhiFuBaoVo;

public class ZhiFuBaoUtil {

// 测试   public static final String severUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    public static final String severUrl = "https://openapi.alipay.com/gateway.do";
// 测试    public static final String appId = "9021000133673820";
    public static final String appId = "2021004131621702";
    //私钥
//  测试  public static final String merchantPrivateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC329/sRsgqjgN6a7XengiITxuxxrliBZfiRXrrUWcE6nrNabqZW38f+z6zckZFJIIrBJP2lUj30ed7SlyP3OvQnXqyKri40xv7Sta6intnSA4B7XdMvm/PvU/VwRqZubhbfheHMwIpVjuKJNLNpmg1xYCyUSCMAATnNSz+eN1MivQzkrhko3cYBONIFA2suBJIx12PeDArtRAIUkJMr8PJttnRIDqufeI/9LA+noGqnP3+Bfnr564sy15nddGX0+pUR/n5K/ZvwjzM77WSbDYf285EI2p8PefR1iNG3nQxUXA5PS2w34Ld1RQWNaZfjxNUU+uBiJvzaSUfmN8zwAVLAgMBAAECggEBAK/dwfNgQVdzQCB5px2qTL8V7IfAbtM0RnRJwaoV01UAdTbHS5YlzxPoX7GGPXEl2t0LI/1aDtH+Fk34c1GpIGgMOrDx7H/eb4WPsJX+oMvKQJvXaaYfAxTTIOp2K9ufqpjZo+fPAuJHeexKDNppsoS8xr3KnDdOTWJbmw5Ozl/HaKiHM0rjcYSIvk7a0iF3VHYSDxb8uIeagqsOqvsN7kiHRVfzqvgFVuxzbMrYXQzxydc1K3RQPZBYSV/YsVaqohOEsA72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fJziP7LuYe2G7Whm0kOyF40J5QF7V8ocsqMZQTTF6zcIQt+upAsUx/QhRI9/YFlhpTao0yRtizHY7yUIA+99vZDYIttPAoGAN6RA+4//jP+0MyVvv1KS5Y4Anw5s9+odYous2rzFgpJBHdnJXTjSVoha310iDz9uPyPqekzkT3l/3CJ6ZQu1tHuI7PffJN0+6kPzerFwkaamIV9TwHrsxB1QJrmFFHSy/yT/qDZOSnbLSc6wgSsCAz+Dt7ltyXJ/FZn4bEEF09ECgYAdG7a0idVkIxwg2N6vAgxlPvv3M0OzqTN3cDITWIuqKkGt0iJt+9X5rpQayUd9KHR8u8Pq94lSdt89U8HchC9IE22zHIdAYkmr4X8xVR1H8wnmmoKM+D4qS334RuGCFxKP8OXS2Dv2gDQPhYYWY/WvH6clQ5TOl0cT5Zcreo9CmA==";
    //支付宝公钥字符串 测试
    public static final String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyh8/Cn+UVMypeeF1gTYkJrwl0APfDNzc9qm4EVge7D0q3pTTKE0h8EkzyEGky9FMBh3Ut3vBcT8yZSc/pSN6oUPEAHDqo10k9oss1a6fNl72WgfAOnHIc+80NLYQA27V6jG2qxNhKgWzotMlb3E0Ng18rPfW67LZG40U8u8s+CQC2Vx2yjYhP4cMXxmtWKcXV5mXtRH0KP8K53fgksI2PlASmmIy4aez4PGGniCP0f8E56IUNtTvzW3UPrXF6kYTM8W6tvA4wVhWdiDBVTxs8auQNjPsAoNAKU9x40fwC1oLw3DinVkP0nDgx/GP0x2IYh0pXyrC+05jofBHx6qKrwIDAQAB";

    //异常通知的回调地址
    private static String notifyUrl = "https://51757b8k27.zicp.fun/api/aliPay/notify";

    public static String pay(ZhiFuBaoVo zhiFuBaoVo) throws AlipayApiException {
        AlipayClient alipayClient = AliPayClientHeader.certClient();
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        request.setNotifyUrl(notifyUrl);
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", zhiFuBaoVo.getOutTradeNo());
        bizContent.put("total_amount", zhiFuBaoVo.getTotalAmount());
        bizContent.put("subject", zhiFuBaoVo.getSubject());
        bizContent.put("product_code", "QUICK_MSECURITY_PAY");

        request.setBizContent(bizContent.toString());
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        String order = response.getBody();
        System.err.println("orderStr "+order);
        if(!response.isSuccess()){
            throw new ServiceException("调用失败", HttpStatus.ERROR);
        }
        return order;




    }

    static class AliPayClientHeader{
        // 付宝证书模式请求 Client
        public static AlipayClient certClient() throws AlipayApiException {
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
            certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");

            certAlipayRequest.setAppId(appId);  //APPID 即创建应用后生成,详情见创建应用并获取 APPID
            certAlipayRequest.setPrivateKey(merchantPrivateKey);  //开发者应用私钥，由开发者自己生成
            certAlipayRequest.setFormat("JSON");  //参数返回格式，只支持 json 格式
            certAlipayRequest.setCharset("utf-8");  //请求和签名使用的字符编码格式，支持 GBK和 UTF-8
            certAlipayRequest.setSignType("RSA2");  //商户生成签名字符串所使用的签名算法类型，目前支持 RSA2 和 RSA，推荐商家使用 RSA2。
//            certAlipayRequest.setCertPath("C:/Users/17251/Desktop/ceshi/appPublicCert.crt"); //应用公钥证书路径（app_cert_path 文件绝对路径）
//            certAlipayRequest.setAlipayPublicCertPath("C:/Users/17251/Desktop/ceshi/alipayPublicCert.crt"); //支付宝公钥证书文件路径（alipay_cert_path 文件绝对路径）
//            certAlipayRequest.setRootCertPath("C:/Users/17251/Desktop/ceshi/alipayRootCert.crt");  //支付宝CA根证书文件路径（alipay_root_cert_path 文件绝对路径）
            certAlipayRequest.setCertPath("C:/Users/17251/Desktop/zhifubao/appCertPublicKey_2021004131621702.crt"); //应用公钥证书路径（app_cert_path 文件绝对路径）
            certAlipayRequest.setAlipayPublicCertPath("C:/Users/17251/Desktop/zhifubao/alipayCertPublicKey_RSA2.crt"); //支付宝公钥证书文件路径（alipay_cert_path 文件绝对路径）
            certAlipayRequest.setRootCertPath("C:/Users/17251/Desktop/zhifubao/alipayRootCert.crt");  //支付宝CA根证书文件路径（alipay_root_cert_path
            AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);

            return alipayClient;


        }
        // 支付宝无证书模式请求 Client
        public static AlipayClient outCertClient() throws AlipayApiException {
            AlipayClient alipayClient = new DefaultAlipayClient(
                    severUrl,
                    appId, merchantPrivateKey, "JSON",
                    "utf-8", alipayPublicKey, "RSA2");
            return alipayClient;
        }
    }




}
